import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:sleep/comp/fatigue/health_state.dart';
import 'package:sleep/db/merroy_db.dart';
import 'package:sleep/help_style.dart';
import 'package:sleep/page/blue/blue_scan_screen.dart';
import 'package:sleep/page/fatigue/comp/fatigue_detail.dart';
import 'package:sleep/page/fatigue/comp/heart_deatil.dart';
import 'package:sleep/page/fatigue/comp/title_chart.dart';
import 'package:sleep/page/fatigue/detail_page.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:sleep/page/user.dart';
import 'package:sleep/provider/chat_config_provider.dart';
import 'package:sleep/provider/db_provider.dart';
import 'package:sleep/provider/wave_deatil_provider/wave_provider.dart';

const name = 'User';

class Fatigue extends StatefulWidget {
  const Fatigue({Key? key}) : super(key: key);

  @override
  State<Fatigue> createState() => _FatigueState();
}

class _FatigueState extends State<Fatigue> {
  @override
  void initState() {
    super.initState();
  }

  jumpDetail() {
    Navigator.of(context)
        .push(CupertinoPageRoute(builder: (c) => const DetailPage()));
  }

  Widget deatilTitle() {
    return Row(children: [
      Text(
        "Details",
        style: HelpStyle.contextStyle,
      ),
      const Expanded(child: SizedBox()),
      GestureDetector(
        behavior: HitTestBehavior.translucent,
        onTap: jumpDetail,
        child: Row(
          children: [
            Text("more", style: HelpStyle.contextStyle),
            Icon(Icons.arrow_forward_ios_outlined,
                size: 16, color: HelpStyle.iconColor)
          ],
        ),
      )
    ]);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(
          "Fatigue",
          style: HelpStyle.titleStyle,
        ),
        actions: [
          Consumer(
            builder: (context, ref, child) {
              return IconButton(
                  padding: EdgeInsets.zero,
                  onPressed: () {
                    ref.read(fatigueProvider).analyze.clear();
                    ref.read(pulseProvider).analyze.clear();
                    MerroyDB.share().sleepClear();
                  },
                  icon: Icon(
                    Icons.refresh,
                    color: HelpStyle.primarycolor,
                  ));
            },
          ),
          Consumer(
            builder: (context, ref, child) {
              final fatigue = ref.watch(fatigueProvider);
              final needSave = fatigue.needSave;
              return IconButton(
                  padding: EdgeInsets.zero,
                  onPressed: () {
                    if (!needSave) EasyLoading.showToast("Recording....");

                    if (needSave) {
                      EasyLoading.showToast("save in Download file");
                    }
                    fatigue.changeSave(!needSave, fatigue: true);
                  },
                  icon: Icon(
                    needSave ? Icons.draw : Icons.mode_edit,
                    color: needSave
                        ? HelpStyle.primarycolor
                        : HelpStyle.grayTitleColor,
                  ));
            },
          ),
          IconButton(
              padding: EdgeInsets.zero,
              onPressed: () {
                Navigator.of(context).push(
                  MaterialPageRoute(builder: (context) => BlueScanScreen()),
                );
              },
              icon: Icon(
                Icons.bluetooth,
                color: HelpStyle.primarycolor,
              )),
        ],
        backgroundColor: Colors.transparent,
        elevation: 0.0,
        leading: Padding(
          padding: const EdgeInsets.all(8.0),
          child: ClipRRect(
            borderRadius: BorderRadius.circular(24),
            child: ColoredBox(
                color: HelpStyle.backgroundColor,
                child: Consumer(
                  builder: (context, ref, child) {
                    final db = ref.read(dbProvider);

                    return IconButton(
                        padding: EdgeInsets.zero,
                        onPressed: () {
                          Navigator.of(context).push(CupertinoPageRoute(
                              builder: (c) => UserPage(
                                    type: UserType.fatigue,
                                    person: db.getPerson(kFatiguePerson),
                                  )));
                        },
                        icon: Icon(
                          Icons.person,
                          color: HelpStyle.primarycolor,
                        ));
                  },
                )),
          ),
        ),
      ),
      backgroundColor: HelpStyle.backgroundColor,
      body: Padding(
        padding: EdgeInsets.only(
          top: 0,
          left: HelpStyle.margin,
          right: HelpStyle.margin,
        ),
        child: SingleChildScrollView(
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              const HealthState(),
              SizedBox(height: HelpStyle.margin),
              Consumer(builder:
                  (BuildContext context, WidgetRef ref, Widget? child) {
                final fatigue = ref.watch(fatigueProvider);
                final chatConfig = ref.watch(chatConfigProvider);
                return TitleChart(
                  icon: Icon(
                    Icons.remove_red_eye_outlined,
                    color: Color(0xff1F463E),
                  ),
                  title: "Blink",
                  spots: fatigue.spots,
                  chatConfig: chatConfig.fatigueChat,
                );
              }),
              SizedBox(height: HelpStyle.margin),
              deatilTitle(),
              SizedBox(height: HelpStyle.cellMargin),
              const FgDetail(),
              SizedBox(height: HelpStyle.margin),
              Consumer(
                builder: (context, ref, child) {
                  final pulse = ref.watch(pulseProvider);
                  final chatConfig = ref.watch(chatConfigProvider);
                  return TitleChart(
                    title: "Pulse",
                    icon: Icon(
                      Icons.favorite,
                      color: Color(0xff652639),
                    ),
                    spots: pulse.spots,
                    chatConfig: chatConfig.pulseChat,
                  );
                },
              ),
              SizedBox(height: HelpStyle.margin),
              const HeartDetail(),
              SizedBox(height: HelpStyle.margin * 2),
            ],
          ),
        ),
      ),
    );
  }
}
